package cn.app.android.sichuanxiaoke.com.xiaokeapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.ConstantUtils;

public class HomePHPActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_php);
        webView= (WebView) findViewById(R.id.mywebview);
        Intent intent = getIntent();
        String url = intent.getStringExtra("URL");
        WebSettings settings = webView.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSupportZoom(true);
        //打开与JS的交互权限
        settings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        //设置js的回调接口
        webView.addJavascriptInterface(this,"android");
        //webView.loadUrl(ConstantUtils.HOME_PHP_URL);
        webView.loadUrl(url);


        //android.GoPayActivity();

    }

    //设置连续两次点击退出（不超过２秒）
    long firstTime;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        //KEYCODE_BACK 手机物理回退键　的常量
        if(keyCode==KeyEvent.KEYCODE_BACK){
            long secondTime = System.currentTimeMillis();
            if(secondTime-firstTime>2000){
                Toast.makeText(this,"再按一次退出",Toast.LENGTH_SHORT).show();
                firstTime=System.currentTimeMillis();//第一次点击时给firstTime　赋值
                return true;
            }else {
                finish();//关闭Activity
            }

        }
        return super.onKeyUp(keyCode, event);
    }

    @JavascriptInterface
    public void GoPayActivity(String price){

        Log.i("phpback"," price---"+price);
        Intent intent=new Intent(this,PayActivity.class);
        intent.putExtra("price",price);
        startActivity(intent);

    }
}
